﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Security.Policy;
using WLYT.Application.Interfaces;
using WLYT.Domain.Entries;
using WLYT.Domain.Model;

namespace WLYT.WMS.Controllers
{
    [ApiController]
    [Authorize]
    public class LogController(IBaseService<LogRecord> logRecordService,
                               IBaseService<WarnLog> warnLogService) : ControllerBase
    {
        /// <summary>
        /// 接口日志
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="url"></param>
        /// <param name="content"></param>
        /// <param name="responseContent"></param>
        /// <param name="isSuccess"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/LogRecord/GetData")]
        public async Task<Table<LogRecord>> GetLogRecords(DateTime startDate, DateTime endDate, string url = "", string content = "", string responseContent = "", bool? isSuccess = null,  int pageSize = 20, int pageIndex = 1)
        {
            RefAsync<int> totalNumber = 0;
            Expressionable<LogRecord> exp = Expressionable.Create<LogRecord>();
            exp.And(p => p.CreateTime >= startDate);
            exp.And(p => p.CreateTime <= endDate);
            exp.AndIF(!string.IsNullOrEmpty(url), p => p.RequestUri.Contains(url));
            exp.AndIF(!string.IsNullOrEmpty(content), p => p.RequestContent.Contains(content));
            exp.AndIF(!string.IsNullOrEmpty(responseContent), p => p.ResponseContent.Contains(responseContent));
            exp.AndIF(isSuccess != null, p => p.IsSuccess == isSuccess);
            var result = await logRecordService.AsQueryable().Where(exp.ToExpression()).SplitTable(startDate, endDate).OrderByDescending(p => p.CreateTime).ToPageListAsync(pageIndex, pageSize, totalNumber);
            return new Table<LogRecord>(result, totalNumber);
        }

        /// <summary>
        /// 设备报警日志
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="type"></param>
        /// <param name="errCode"></param>
        /// <param name="message"></param>
        /// <param name="stationCode"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/WarnLog/GetData")]
        public async Task<Table<WarnLog>> GetWarnLogs(DateTime startDate, DateTime endDate, int? type = null, string errCode = "", string message = "", string stationCode = "", int pageSize = 20, int pageIndex = 1)
        {
            RefAsync<int> totalNumber = 0;
            Expressionable<WarnLog> exp = Expressionable.Create<WarnLog>();
            exp.And(p => p.CreateTime >= startDate);
            exp.And(p => p.CreateTime <= endDate);
            exp.AndIF(type != null, p => p.Type == type);
            exp.AndIF(!string.IsNullOrEmpty(errCode), p => p.ErrorCode.Contains(errCode));
            exp.AndIF(!string.IsNullOrEmpty(message), p => p.Message.Contains(message));
            exp.AndIF(!string.IsNullOrEmpty(stationCode), p => p.StationCode.Contains(stationCode));
            var result = await warnLogService.AsQueryable().Where(exp.ToExpression()).SplitTable(startDate, endDate).OrderByDescending(p => p.CreateTime).ToPageListAsync(pageIndex, pageSize, totalNumber);
            return new Table<WarnLog>(result, totalNumber);
        }
    }
}
